package idv.arthur.jenkins;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class GetConsole {

	public static void main(String[] args) throws Exception {
		// 
		String json = readUrl("http://10.144.97.99:8330/jenkins/job/3B/53/logText/progressiveText?start=0");
		
		System.out.println(json);
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			System.out.print("Connecting...");
			System.out.println("OK");
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://10.144.136.54:3306/sysflow", "Arthur",
					"abcd1234");
			
			String sql = "INSERT INTO test (aaa) VALUES (?)";
			
			ps = conn.prepareStatement(sql);
			ps.setString(1, json);
			ps.execute();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) { rs.close(); }
				if (ps != null) { ps.close(); }
				if (conn != null) { conn.close(); }
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	private static String readUrl(String urlString) throws Exception {
		BufferedReader reader = null;
		InputStreamReader insr = null;
		try {
			URL url = new URL(urlString);
			insr = new InputStreamReader(url.openStream());
			reader = new BufferedReader( insr );
			StringBuffer buffer = new StringBuffer();
			int read;
			char[] chars = new char[1024];
			while ((read = reader.read(chars)) != -1) {
				buffer.append(chars, 0, read);
			}
			return buffer.toString();
		} finally {
			
			if (reader != null) {reader.close();}
			if (insr != null) { insr.close(); }
		}
	}
}
